<template>
  <div>
    <el-tree
      ref="tree"
      node-key="id"
      :data="treeList"
      :props="{ label: 'name' }"
      default-expand-all
      :show-checkbox="true"
      :check-strictly="true"
    />
    <el-button @click="hCancel">取消</el-button>
    <el-button type="primary" @click="hSubmit">确认</el-button>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { transDataToTree } from '@/utils'
import { getRoleDetail, assignPerm } from '@/api/setting'

export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      treeList: []
    }
  },

  created() {
    this.loadPermissionList()
  },
  methods: {
    hCancel() {
      this.$emit('close')
    },
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        console.log('getPermissionList', res)
        this.treeList = transDataToTree(res.data)
        // 保存数据
        const info = await getRoleDetail(this.id)
        // data.info.permIds ------权限点的id
        this.$refs.tree.setCheckedKeys(info.data.permIds)
        console.log('getRoleDetail', info)
      } catch (err) {
        console.log('getPermissionList', err)
      }
    },
    async hSubmit() {
      try {
        const permIds = this.$refs.tree.getCheckedKeys()
        const res = await assignPerm({ id: this.id, permIds: permIds })
        console.log('assignPerm', res)
        // 保存数据
        // this.$emit('close')
        this.hCancel()
      } catch (err) {
        console.log('assignPerm', err)
      }
    }
  }
}
</script>

<style></style>
